package com.api.devices.controller;

import com.api.common.core.controller.BaseController;
import com.api.common.core.domain.AjaxResult;
import com.api.common.core.page.TableDataInfo;
import com.api.devices.domain.vo.MonitoredVo;
import com.api.devices.service.IMonitoredService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/monitored")
public class MonitoredController extends BaseController {
    @Autowired
    private IMonitoredService monitoredService;

    @PostMapping("/list")
    public TableDataInfo list(@RequestHeader("App-Language") String appLang,
                              @RequestBody MonitoredVo vo){
        vo.setAppLang(appLang);
        if(ObjectUtils.isEmpty(vo.getIpcCodeList())) vo.setIpcCodeList(null);
        if(ObjectUtils.isEmpty(vo.getGroupIdList())) vo.setGroupIdList(null);
        if(vo.getPageNum()>0) {
            startPageByPost(vo.getPageNum(), vo.getPageSize());
        }
        return getDataTable(monitoredService.list(vo));
    }

    @PostMapping("info")
    public AjaxResult info(@RequestHeader("App-Language") String appLang,
                           @RequestBody MonitoredVo vo){
        try {
            return AjaxResult.success(monitoredService.item(vo));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("create")
    public AjaxResult add(@RequestHeader("App-Language") String appLang,
                           @RequestBody MonitoredVo vo){
        vo.setCreateBy(getLoginUser().getUser().getUserName());
        try {
            return AjaxResult.success(monitoredService.create(vo));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @PostMapping("modify")
    public AjaxResult modify(@RequestHeader("App-Language") String appLang,
                          @RequestBody MonitoredVo vo){
        vo.setUpdateBy(getLoginUser().getUser().getUserName());
        try {
            return AjaxResult.success(monitoredService.modify(vo));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @PostMapping("remove")
    public AjaxResult remove(@RequestBody MonitoredVo vo){
        try {
            monitoredService.delete(vo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
